#!/bin/bash
MODULE=${PROJECT_NAME}
if [ -n "$MODULE" ]; then
    echo "MODULE: "$MODULE
else
    echo 'MODULE为空'
    exit 2
fi
DOCKER_CONTAINER_NAME=$(docker ps -q --filter "name=$MODULE")
echo '$DOCKER_CONTAINER_NAME:' $DOCKER_CONTAINER_NAME
# 定义变量
TAR_FILE_PATH=dist.tar.gz  # 本地压缩文件路径
DESTINATION_PATH=/usr/share/nginx/html  # 容器内目标目录
# 1. 清理 Docker 容器
echo "# 1. 清理 Docker 容器文件夹："$DESTINATION_PATH
docker exec $DOCKER_CONTAINER_NAME bash -c "rm -rf $DESTINATION_PATH/*"

# 2. 将压缩文件复制到 Docker 容器内
echo "# 2. 将压缩文件$TAR_FILE_PATH复制到 Docker 容器中"
docker cp $TAR_FILE_PATH "$DOCKER_CONTAINER_NAME:$DESTINATION_PATH"

# 3. 进入容器并解压缩文件
echo "# 3. 进入容器并解压缩文件..."$TAR_FILE_PATH
docker exec $DOCKER_CONTAINER_NAME tar -xzvf $DESTINATION_PATH/$(basename $TAR_FILE_PATH) -C $DESTINATION_PATH
docker exec $DOCKER_CONTAINER_NAME bash -c "mv $DESTINATION_PATH/dist/* $DESTINATION_PATH"

# 4. 删除压缩文件（可选）
echo "# 4. 删除容器内的压缩文件"$TAR_FILE_PATH
docker exec $DOCKER_CONTAINER_NAME rm "$DESTINATION_PATH/$(basename $TAR_FILE_PATH)"

docker exec $DOCKER_CONTAINER_NAME rm -f /etc/nginx/nginx.conf
docker exec $DOCKER_CONTAINER_NAME rm -f /etc/nginx/conf.d/default.conf

# 5. 复制 nginx 文件
echo "# 5. 复制 nginx 文件"
docker cp "nginx.conf" "$DOCKER_CONTAINER_NAME:/etc/nginx/"
docker cp "http.conf" "$DOCKER_CONTAINER_NAME:/etc/nginx/conf.d/"

# 6. 重启容器
echo "# 6. 重启容器"
docker restart $DOCKER_CONTAINER_NAME

echo "Complete!"
